package com.itfreer.notice;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.itfreer.form.api.BaseRestService;
import com.itfreer.utils.json.JsonUtils;

/**
 * 定义通知记录接口
 */
@Component
@Path("/noticelog/")
public class RestNoticeLogService {
	
	@Autowired
	private NoticeLogService noticeLogService;

	/**
	 * 获取实体列表
	 * 
	 * @param queryFields
	 *            查询字段
	 * @param where
	 *            查询条件
	 * @param order
	 *            排序条件
	 * @param pageSize
	 *            分页大小
	 * @param pageIndex
	 *            分页索引
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getEntitys")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public List<NoticeLogEntity> getEntitys(String data) throws IOException {
		HashMap<String, Object> args = BaseRestService.getRestParam(data);
		String queryFields = null;
		String whereString = null;
		String orderString = null;
		Integer pageSize = 10;
		Integer pageIndex = 1;

		if (args != null) {
			if (args.containsKey("queryFields") && args.get("queryFields") != null)
				queryFields = args.get("queryFields").toString();
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
			if (args.containsKey("order") && args.get("order") != null)
				orderString = args.get("order").toString();
			if (args.containsKey("pageSize") && args.get("pageSize") != null)
				pageSize = new Integer(args.get("pageSize").toString());
			if (args.containsKey("pageIndex") && args.get("pageIndex") != null)
				pageIndex = new Integer(args.get("pageIndex").toString());
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		HashMap order = JsonUtils.toBean(orderString, HashMap.class);
		return noticeLogService.getEntitys(queryFields, where, order, pageSize, pageIndex);
	}

	/**
	 * 获取当前条件下的数据行总数
	 * 
	 * @param where
	 *            查询条件
	 * @return
	 * @throws IOException
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@POST
	@Path("/getRowCounts")
	@Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON)
	public Long getRowCounts(String data) throws IOException {
		HashMap<String, Object> args = BaseRestService.getRestParam(data);
		String whereString = null;
		if (args != null) {
			if (args.containsKey("where") && args.get("where") != null)
				whereString = args.get("where").toString();
		}
		HashMap where = JsonUtils.toBean(whereString, HashMap.class);
		return noticeLogService.getRowCounts(where);
	}
}